﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewViews.Models;
using System.IO;
using System.Data.Entity;
using System.Data;
namespace NewViews.Controllers
{
    public class AdminController : Controller
    {
        EShopDataEntities5 dbEntities = new EShopDataEntities5();
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ShowReviews()
        {
            object value = Session["key"];
            if (value != null)
            {
                var User_Reviews = dbEntities.ReViews;
                ViewBag.Messages = User_Reviews.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account"); 
        }
        public ActionResult AdminHome()
        {
            object value = Session["key"];
            if (value != null)
            {
               
                var products = dbEntities.Products;
                ViewBag.product = products.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account"); 
           
        }
        public ActionResult Gallery()
        {
            object value = Session["key"];
            if (value != null)
            {

                var products = dbEntities.Products;
                ViewBag.X = products.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account"); 
        }
        public ActionResult AddProduct()
        {
            object value = Session["key"];
            if (value != null)
            {
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult AddProductDB(HttpPostedFileBase file)
        {
            object value = Session["key"];
            if (value != null)
            {

                var prod_data = dbEntities.Products;
            Product new_pro = new Product();
            new_pro.Product_Name = Request["category"];
            new_pro.Product_Company = Request["p_company"];
            new_pro.Product_Price = Convert.ToInt32(Request["p_price"]);
            new_pro.Product_Quantity = Convert.ToInt32(Request["p_quantity"]);
            new_pro.Product_Color = Request["p_color"];
            if (file != null && file.ContentLength > 0)
            {
                new_pro.Product_Image = file.FileName;
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
                file.SaveAs(path);
            }
            dbEntities.Products.Add(new_pro);
            dbEntities.SaveChanges();
               return RedirectToAction("AdminHome", "Admin");
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult UpdateProduct(int id=0)
        {
            object value = Session["key"];
            if (value != null)
            {
                
                var products = dbEntities.Products;
                ViewBag.prod = products.Find(id);
                Product prouct_update = products.Find(id);
                if (prouct_update == null)
                {
                    return HttpNotFound();
                }
                return View(prouct_update);
            }    
            return RedirectToAction("Login", "Account");
            
        }
        [HttpPost]
        public ActionResult UpdateProductDB(Product update_prod)
        {
            object value = Session["key"];
            if (value != null)
            {
            Product new_pro = new Product();
            if (ModelState.IsValid)
            {
                dbEntities.Entry(update_prod).State = EntityState.Modified;
                dbEntities.SaveChanges();
                return RedirectToAction("AdminHome");
            }
            return View(update_prod);
            }
            return RedirectToAction("Login", "Account");
           
        }
     
        public ActionResult DeleteProduct(int id = 0)
        {
             object value = Session["key"];
            if (value != null)
            {

                var products = dbEntities.Products;
            ViewBag.prod = products.Find(id);
            Product dbPprod = products.Find(id);
            if (dbPprod == null)
            {
                return HttpNotFound();
            }
            return View(dbPprod);
            }
            return RedirectToAction("Login", "Account");

        }
        [HttpPost, ActionName("DeleteProduct")]
        public ActionResult DeleteProductDB(int id)
        {
            object value = Session["key"];
            if (value != null)
            {
                
                var products = dbEntities.Products;
            ViewBag.X = products.Find(id);
            Product product = products.Find(id);
            products.Remove(product);
            dbEntities.SaveChanges();
            return RedirectToAction("AdminHome");
            }
            return RedirectToAction("Login", "Account");

        }
        
        
    }
}
